package com.lvy.hczn.front.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.util.StringUtil;
import com.lvy.hczn.front.common.constant.Constants;
import com.lvy.hczn.front.common.core.text.Convert;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.front.common.utils.StringUtils;
import com.lvy.hczn.front.system.domain.system.SupplierBoxPrefix;
import com.lvy.hczn.front.system.mapper.SupplierBoxPrefixMapper;
import com.lvy.hczn.front.system.service.SupplierBoxPrefixService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SupplierBoxPrefixServiceImpl extends ServiceImpl<SupplierBoxPrefixMapper, SupplierBoxPrefix> implements SupplierBoxPrefixService {

    @Autowired
    private SupplierBoxPrefixMapper supplierBoxPrefixMapper;

    /**
     * 查询供应商货箱前缀管理
     *
     * @param id 供应商货箱前缀管理主键
     * @return 供应商货箱前缀管理
     */
    @Override
    public SupplierBoxPrefix selectSupplierBoxPrefixById(String id) {
        return supplierBoxPrefixMapper.selectById(id);
    }

    /**
     * 查询供应商货箱前缀管理列表
     *
     * @param supplierBoxPrefix 供应商货箱前缀管理
     * @return 供应商货箱前缀管理
     */
    @Override
    public List<SupplierBoxPrefix> selectSupplierBoxPrefixList(SupplierBoxPrefix supplierBoxPrefix) {
        return supplierBoxPrefixMapper.selectSupplierBoxPrefixList(supplierBoxPrefix);
    }

    /**
     * 新增供应商货箱前缀管理
     *
     * @param supplierBoxPrefix 供应商货箱前缀管理
     * @return 结果
     */
    @Override
    public int insertSupplierBoxPrefix(SupplierBoxPrefix supplierBoxPrefix) {
        if (supplierBoxPrefix != null) {
            supplierBoxPrefix.setOccupy(Constants.OCCUPY_FALSE);
        }
        boolean flag = this.checkPrefixUnique(supplierBoxPrefix);
        if (!flag) {
            throw new UtilException("前缀已存在");
        }
        return supplierBoxPrefixMapper.insert(supplierBoxPrefix);
    }

    public void init() {
        List<SupplierBoxPrefix> list = new ArrayList<>();
        for (int i = 0; i < Constants.ALPHABET.length; i++) {
            for (int j = 0; j < Constants.ALPHABET.length; j++) {
                SupplierBoxPrefix prefix = new SupplierBoxPrefix();
                prefix.setOccupy("0");
                prefix.setPrefix(Constants.ALPHABET[i] + Constants.ALPHABET[j]);
                list.add(prefix);
            }
        }
        super.saveBatch(list);
    }

    /**
     * 修改供应商货箱前缀管理
     *
     * @param supplierBoxPrefix 供应商货箱前缀管理
     * @return 结果
     */
    @Override
    public int updateSupplierBoxPrefix(SupplierBoxPrefix supplierBoxPrefix) {
        if(!StringUtils.isEmpty(supplierBoxPrefix.getSupplierCode())){
            supplierBoxPrefix.setOccupy(Constants.OCCUPY_TRUE);
        }
        return supplierBoxPrefixMapper.updateById(supplierBoxPrefix);
    }

    /**
     * 批量删除供应商货箱前缀管理
     *
     * @param ids 需要删除的供应商货箱前缀管理主键
     * @return 结果
     */
    @Override
    public int deleteSupplierBoxPrefixByIds(String ids) {
        String[] arr = Convert.toStrArray(ids);
        for (int i = 0; i < arr.length; i++) {
            super.removeById(arr[i]);
        }
        return arr.length;
    }

    /**
     * 删除供应商货箱前缀管理信息
     *
     * @param id 供应商货箱前缀管理主键
     * @return 结果
     */
    @Override
    public int deleteSupplierBoxPrefixById(String id) {
        boolean flag = super.removeById(id);
        if (flag) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 校验前缀是否存在
     *
     * @param supplierBoxPrefix
     * @return
     */
    @Override
    public boolean checkPrefixUnique(SupplierBoxPrefix supplierBoxPrefix) {
        if (StringUtil.isEmpty(supplierBoxPrefix.getPrefix())) {
            return false;
        }
        SupplierBoxPrefix prefix = super.getOne(Wrappers.<SupplierBoxPrefix>lambdaQuery().eq(SupplierBoxPrefix::getPrefix, supplierBoxPrefix.getPrefix()).last("limit 1"));
        if (prefix != null) {
            return false;
        }
        return true;
    }
}
